package com.siruier.boss.ui.adapter

import com.siruier.boss.R
import com.siruier.boss.bean.ConfirmOrderGoodsBean
import com.siruier.boss.bean.OrderDetailBean
import com.siruier.boss.common.buildPrice
import com.siruier.boss.common.formatPrice
import com.siruier.boss.common.formatPriceRmb
import com.siruier.boss.common.toArray
import com.siruier.boss.databinding.ItemConfirmOrderShopBinding
import com.siruier.boss.databinding.ItemConfirmOrderShopGoodsBinding
import com.siruier.boss.ui.base.BaseRecyclerAdapter
import com.siruier.boss.ui.ext.getString
import com.siruier.boss.ui.ext.textSpan
import com.siruier.boss.ui.ext.toColor
import com.siruier.boss.ui.utils.FullyLinearLayoutManager
import com.siruier.boss.ui.utils.GlideUtils

class ConfirmOrderShopAdapter : BaseRecyclerAdapter<ItemConfirmOrderShopBinding, OrderDetailBean>() {
    companion object {
        val UPDATE_NOTE_PAYLOAD = Any()// 更新订单备注
    }

    override fun onBindViewHolder(vb: ItemConfirmOrderShopBinding, bean: OrderDetailBean, position: Int) {}

    override fun onBindViewHolder(vb: ItemConfirmOrderShopBinding, bean: OrderDetailBean, position: Int, payload: Any?) {
        when (payload) {
            UPDATE_NOTE_PAYLOAD -> vb.run { tvNote.text = bean.comment }
            else -> {
                vb.run {
                    bean.run {
                        tvShopName.text = storeName.ifEmpty { getString(R.string.app_name) }
                        var mConfirmOrderShopSpecAdapter = rvGoods.adapter as ConfirmOrderShopSpecAdapter?
                        if (mConfirmOrderShopSpecAdapter == null) {
                            mConfirmOrderShopSpecAdapter = ConfirmOrderShopSpecAdapter().apply { setOnClickAdapterListenter(mOnClickAdapterListenter) }
                            rvGoods.adapter = mConfirmOrderShopSpecAdapter
                            rvGoods.layoutManager = FullyLinearLayoutManager(rvGoods.context)
                        }
                        mConfirmOrderShopSpecAdapter.datas = goodsList
                        mConfirmOrderShopSpecAdapter.notifyDataSetChanged()
                        tvNote.text = comment
                        tvDistributionFreight.text = freightPrice.formatPriceRmb()
                        // 没有购物车就只有一件商品,增加购物车需要修改
                        tvPriceNote.textSpan("共${goodsList[0].number}件 实付：￥${buildPrice(orderPrice.add(freightPrice))}") {
                            spanText = "实付："
                            addForegroundColorSpan(R.color.text_color.toColor())
                            spanText = "￥${buildPrice(orderPrice)}"
                            addForegroundColorSpan(R.color.mall_main.toColor())
                        }
                        setViewClick(position, tvNote)
                    }
                }
            }
        }
    }

}

class ConfirmOrderShopSpecAdapter : BaseRecyclerAdapter<ItemConfirmOrderShopGoodsBinding, ConfirmOrderGoodsBean>() {
    override fun onBindViewHolder(vb: ItemConfirmOrderShopGoodsBinding, bean: ConfirmOrderGoodsBean, position: Int) {
        vb.run {
            bean.run {
                GlideUtils.intoRoundedCorners(ivGoodsImage.context, picUrl, ivGoodsImage, roundingRadius = 5)
                tvGoodsName.text = goodsName
                tvSpec.text = getString(R.string.text_shopping_cart_spec, specifications.toArray(String::class.java).joinToString(" "))
                tvMemberPrice.text = priceDesc
                tvNumber.text = "x${number}"
                setViewClick(position, root)
            }
        }
    }
}